<?php

/*
 * Edit by shengxin
 * Click nbfs://nbhost/SystemFileSystem/Templates/Scripting/PHPClass.php to edit this template
 */

namespace api\admin\controller;

use cmf\controller\RestAdminBaseController;
use api\mall\model\BoothModel;
/**
 * Description of DigitalController
 *
 * @author PC
 */
class DigitalController extends RestAdminBaseController{
    public function add(){
        $post = $this->check_post(false);

        $boothModel = new BoothModel();
        if(!isset($post['check_count'])){
            $post['check_count'] = 1;
        }
        $boothModel->insert($post);
        $this->success('ok',$post);
    }
    
    public function save(){
        $post = $this->check_post(true);
        $boothModel = new BoothModel();
        $post['use_item_icon'] = $this->request->post('use_item_icon','0')=='on'?1:0;
        $boothModel->where('id',$post['id'])->update($post);
        $this->success('ok',$post);
    }
    
    private function check_post($edit){
        $post = $this->request->post();
        $validate = [
            'booth_name'=>'require|length:2,10',
            'button_name'=>'require|length:2,10',
//            'item_num'=>'require|integer|gt:0',
            'booth_class'=>'require|length:1,20',
//            'item_images'=>'require|lenght:2,60',
//            'item_price'=>'require|integer|gt:0'
        ];
        $msgs = [
            'booth_name.require'=>'展位名称不能为空',
            'booth_name.length'=>'展位名称长度不符合要求需2-10位',

            'button_name.require'=>'展位按钮不能为空',
            'button_name.length'=>'展位按钮长度不符合要求需2-10位',

            'booth_class.require'=>'展位类型不能为空',
            'booth_class.length'=>'展位类型长度不符合要求需2-10位'

        ];
        if($edit){
            unset($validate['booth_name']);
            unset($validate['booth_class']);
            unset($post['booth_name']);
        }
        $result = $this->validate($post, $validate,$msgs);
        if($result!==true){
            $this->error($result,$post);
        }
        return $post;
    }
}
